import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/bean/project_data.dart';
import 'package:flutter_app/compents/state_page.dart';
import 'package:flutter_app/controller/project/project_controller.dart';
import 'package:flutter_app/page/project/project_content_page.dart';
import 'package:get/get.dart';
class ProjectPage extends StatefulWidget {
  @override
  _ProjectPageState createState() => _ProjectPageState();
}

class _ProjectPageState extends State<ProjectPage> with TickerProviderStateMixin {

  TabController _tabController;
  var _projectController = Get.put<ProjectController>(ProjectController());

  @override
  Widget build(BuildContext context) {
   return DefaultTabController(
     length: _projectController.projects.length,
     child: Scaffold(
       appBar: AppBar(
         brightness: Brightness.dark,
         title: Container(
           width: double.infinity,
           child: Obx(()=>_buildProjectTabs(_projectController.projects)),
         ),
       ),
       body: GetX<ProjectController>(
         initState: (_){
           _projectController.getProjectTabs();
         },
         builder: (_){
           _tabController = TabController(
               length: _projectController.projects.length,
               initialIndex: _projectController.iniItemIndex,
               vsync: this);
           _tabController.addListener(() {
             _projectController.iniItemIndex = _tabController.index;
           });
           return StateCommonPage<ProjectController>(
             model: _projectController,
             onPressed: (){
               _projectController.getProjectTabs();
             },
             child: TabBarView(
               controller: _tabController,
               children: _projectController.projects.map((e) {
                 return  ProjectContentPage(e.id.toString());
               }).toList(),
             ),
           );
         },
       ),
     ),
   );
  }


  Widget _buildProjectTabs(List<Project> projects){
    return TabBar(
      isScrollable: true,
      controller: _tabController,
      tabs: projects.map((e) {
       return Tab(text: e.name);
      }).toList(),
    );
  }
}
